package com.haidechizi.leetcode;

/**
 * @author: wangdejun
 * @create: 2020-04-10 09:33
 **/
public class _151_翻转字符串里的单词 {

    public String reverseWords(String s) {
        if (s == null || "".equals(s)) {
            return s;
        }
        String[] s1 = s.split(" ");
        StringBuilder sb = new StringBuilder();
        for (int i = s1.length - 1; i >= 0; i--) {
            if (s1[i] != null && !"".equals(s1[i])) {
                sb.append(s1[i]);
                sb.append(" ");
            }
        }
        if ("".equals(sb.toString())) {
            return "";
        }
        String substring = sb.substring(0, sb.length() - 1);
        return substring;
    }

    public static void main(String[] args) {
//        System.out.println(new _151_翻转字符串里的单词().reverseWords("   s9   c9 suv    0   "));
        System.out.println(new _151_翻转字符串里的单词().reverseWords(" "));
    }
}
